//
//  TJSelectPayWayController.m
//  Teleport
//
//  Created by hc-101 on 2020/6/16.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJSinglePayWayController.h"

@interface TJSinglePayWayController ()<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) NSMutableArray *datas;


@property (strong, nonatomic) UITableView *tableView;
@property (weak, nonatomic) UIView *maskView;
@property (weak, nonatomic) UIView *contentView;

@end


@implementation TJSinglePayWayController



- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _datas = @[].mutableCopy;
    
    
    if (self.model.payMap.WECHAT.url.replacingEmptyString.length) {
        TJCellStyleModel *weChatModel =  [TJCellStyleModel cellStyleModelWithText:@"微信".localizedString detailText:@"" cellClass:@"TJStyle1Cell"];
        weChatModel.icon = @"OTC_list_weChat";
        weChatModel.identification = @"WECHAT";
        weChatModel.name = self.model.payMap.WECHAT.name;
        weChatModel.uname = self.model.payMap.WECHAT.uname;
        weChatModel.url = self.model.payMap.WECHAT.url;
        [_datas addObject:weChatModel];
    }
    
    if (self.model.payMap.ALIAY.url.replacingEmptyString.length) {
        
        TJCellStyleModel *alipayModel =  [TJCellStyleModel cellStyleModelWithText:@"支付宝".localizedString detailText:@"" cellClass:@"TJStyle1Cell"];
        alipayModel.icon = @"OTC_list_Alipay";
        alipayModel.identification = @"ALIAY";
        alipayModel.name = self.model.payMap.ALIAY.name;
        alipayModel.uname = self.model.payMap.ALIAY.uname;
        alipayModel.url = self.model.payMap.ALIAY.url;
        [_datas addObject:alipayModel];
    }
    
    
    if (self.model.payMap.BANKCARD.bankCard.replacingEmptyString.length) {
        
        TJCellStyleModel *bankModel =  [TJCellStyleModel cellStyleModelWithText:@"银行卡".localizedString detailText:@"" cellClass:@"TJStyle1Cell"];
        bankModel.icon = @"OTC_list_bank";
        bankModel.identification = @"BANKCARD";
        bankModel.name = self.model.payMap.BANKCARD.name;
        bankModel.bankCard = self.model.payMap.BANKCARD.bankCard;
        bankModel.bankName = self.model.payMap.BANKCARD.bankName;
        bankModel.bankAddress = self.model.payMap.BANKCARD.bankAddress;
        bankModel.uname = self.model.payMap.BANKCARD.name;
        [_datas addObject:bankModel];
    }
    
    
    
    self.view.backgroundColor = UIColor.clearColor;
    
    UIView *maskView = [UIView tj_addToView:self.view];
    maskView.backgroundColor = [UIColor.tj_alertMaskColor colorWithAlphaComponent:0];
    maskView.frame = self.view.bounds;
    
    UIView *contentView = [UIView tj_addToView:maskView];
    contentView.backgroundColor = UIColor.whiteColor;
    UIView *headerView = [UIView tj_addToView:contentView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(50);
        make.top.offset(0);
    }];
    
    UIView *lineView = [UIView tj_addToView:headerView];
    lineView.backgroundColor = UIColor.tj_separatorColor;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(0);
        make.left.offset(16);
        make.bottom.offset(0);
        make.height.offset(0.7);
    }];
    
    UILabel *nameLabel = [UILabel lableWithSuperView:headerView font:MediumFont(17) color:UIColor.blackColor title:@"请选择支付方式".localizedString textAlignment:NSTextAlignmentLeft];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(16);
    }];
    
    UIButton *dismissButton = [UIButton buttonWithSuperView:headerView font:UIFont_Px(15) color:UIColor_Hex(0x272727) title:@"关闭".localizedString];
    [dismissButton addTarget:self action:@selector(didClickDismissButton) forControlEvents:UIControlEventTouchUpInside];
    [dismissButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentRight)];
    [dismissButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(nameLabel);
        make.width.height.offset(40);
        make.right.offset(-16);
    }];
    
    
    [contentView addSubview:self.tableView];
    CGFloat tableViewHeight = 3 * self.tableView.rowHeight;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(tableViewHeight);
        make.top.equalTo(headerView.mas_bottom);
    }];
    
    tableViewHeight = MAX(tableViewHeight, self.tableView.rowHeight * 3);
    
    CGFloat totalHeight = tableViewHeight + TabbarSafeBottomMargin + 50;
    contentView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, totalHeight);
    
    _maskView = maskView;
    _contentView = contentView;
    
}

- (void)didClickDismissButton {
    [self dismissComplete:nil];
}


- (void)dismissComplete:(void (^)(void))complete {
    
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        self.contentView.tj_y = KScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (complete) complete();
        }];
    }];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = UIColor.tj_alertMaskColor;
        self.contentView.tj_y = KScreenHeight - self.contentView.tj_height;
    } completion:^(BOOL finished) {
    }];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerClass:TJStyle1Cell.class forCellReuseIdentifier:style1Cell];
        _tableView.rowHeight = 49;
        _tableView.scrollEnabled = NO;
        _tableView.bounces = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.estimatedSectionHeaderHeight = 0;
            _tableView.estimatedSectionFooterHeight = 0;
            _tableView.estimatedRowHeight = 0;
        }
    }
    return _tableView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJStyle1Cell *cell = [tableView dequeueReusableCellWithIdentifier:style1Cell forIndexPath:indexPath];
    cell.model = self.datas[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self dismissComplete:^{
        if (self.selectComplete) {
            self.selectComplete(self.datas[indexPath.row]);
        }
    }];
}




@end
